package com.onlyphoto.onlymall.utils;

import android.os.Environment;
import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by gaochujia on 2019-06-27.
 */

public class StringUtil {

    /**
     * 判断字符串是否为空
     *
     * @return
     */
    public static boolean isBlank(String str) {
        return (str == null || str.length()==0 || "null".equals(str));
    }

    /**
     * 获取sd卡路径
     *
     * @return
     */
    public static String getSdCardPath() {
        return Environment.getExternalStorageDirectory().getAbsolutePath();
    }

    // 判断是否是手机号码
    public static boolean isMobileNO(String mobiles) {
        if (isBlank(mobiles))
            return false;
        Pattern p = Pattern.compile("^(86)?1[0-9]{10}$");
        // ^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    /***
     * 获取url 指定name的value;
     * @param url
     * @param name
     * @return
     */
    public static String getValueByName(String url, String name) {
        String result = "";
        int index = url.indexOf("?");
        String temp = url.substring(index + 1);
        String[] keyValue = temp.split("&");
        for (String str : keyValue) {
            if (str.contains(name)) {
                result = str.replace(name + "=", "");
                break;
            }
        }
        return result;
    }

    /**
     * 两个版本号比较  true需要更新 false不需要更新  mVersion<sVersion
     * @param mVersion
     * @param sVersion
     * @return      true需要更新 false不需要更新
     */
    public static boolean compareAppVersion(String mVersion,String sVersion)
    {
        String[] strs1 = mVersion.split("\\.");
        String[] strs2 = sVersion.split("\\.");

        int length = strs1.length > strs2.length ? strs1.length : strs2.length;

        for(int i = 0; i < length ;i++){
            int vi1 = 0;
            int vi2 = 0;
            if(strs1.length >= i+1){
                vi1 = Integer.parseInt(strs1[i]);
            }
            if(strs2.length >= i+1){
                vi2 = Integer.parseInt(strs2[i]);
            }
            if(vi1 < vi2){
                return true;
            }else if(vi1 > vi2){
                return false;
            }
        }
        return false;
    }

    /**
     * 隐藏手机号码中间四位
     */
    public static String getHiddenPhone(String s) {
        if (!TextUtils.isEmpty(s) && s.length() == 11) {
            return s.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        } else {
            return "";
        }

    }
}
